package com.wanandroidsdk.http;

import com.wanandroidsdk.bean.ArticleListBean;
import com.wanandroidsdk.bean.BannerBean;
import com.wanandroidsdk.bean.DataBean;
import com.wanandroidsdk.bean.HotKeyBean;
import com.wanandroidsdk.bean.KnowledgeBean;
import com.wanandroidsdk.http.api.Api;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: njb
 * @date: 2021/2/23 17:55
 * @desc: 网络请求工具类
 */
public class WanAndroidHttpUtil {

    public static void getBanner(final WanHttpCallBack callBack) {
        (Api.getDefault().banner()).enqueue(new Callback<WanAndroidHttpResult<List<DataBean>>>() {
            @Override
            public void onResponse(@NotNull Call<WanAndroidHttpResult<List<DataBean>>> call, @NotNull Response<WanAndroidHttpResult<List<DataBean>>> response) {
                WanAndroidHttpResult<List<DataBean>> result = response.body();
                if (result != null && result.isBizSucceed(true) && result.getData() != null) {
                    callBack.onResponse(result.getData());
                }
            }

            @Override
            public void onFailure(@NotNull Call<WanAndroidHttpResult<List<DataBean>>> call, @NotNull Throwable t) {
                callBack.onFailure(t.getMessage());
            }
        });
    }

    public static void getArticle(int page, final WanHttpCallBack callBack) {
        Api.getDefault().articleList(page).enqueue(new Callback<WanAndroidHttpResult<ArticleListBean>>() {
            @Override
            public void onResponse(@NotNull Call<WanAndroidHttpResult<ArticleListBean>> call, Response<WanAndroidHttpResult<ArticleListBean>> response) {
                WanAndroidHttpResult<ArticleListBean> result = response.body();
                if (result != null && result.isBizSucceed(true)) {
                    try {
                        callBack.onResponse(result.getData().datas);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<WanAndroidHttpResult<ArticleListBean>> call, Throwable t) {
                callBack.onFailure(t.getMessage());
            }
        });
    }

    public static void getHotKey(final WanHttpCallBack callBack){
        Api.getDefault().getHotKey().enqueue(new Callback<WanAndroidHttpResult<List<DataBean>>>() {
            @Override
            public void onResponse(@NotNull Call<WanAndroidHttpResult<List<DataBean>>> call, @NotNull Response<WanAndroidHttpResult<List<DataBean>>> response) {
                WanAndroidHttpResult<List<DataBean>> result  =response.body();
                if (result != null && result.isBizSucceed(true) && result.getData() != null) {
                    callBack.onResponse(result.getData());
                }
            }

            @Override
            public void onFailure(@NotNull Call<WanAndroidHttpResult<List<DataBean>>> call, @NotNull Throwable t) {
                callBack.onFailure(t.getMessage());
            }
        });
    }

    public static void getKnwLedge(final WanHttpCallBack callBack){
        Api.getDefault().getKnowLedge().enqueue(new Callback<WanAndroidHttpResult<List<DataBean>>>() {
            @Override
            public void onResponse(@NotNull Call<WanAndroidHttpResult<List<DataBean>>> call, @NotNull Response<WanAndroidHttpResult<List<DataBean>>> response) {
                WanAndroidHttpResult<List<DataBean>> result  = response.body();
                if (result != null && result.isBizSucceed(true) && result.getData() != null) {
                    callBack.onResponse(result.getData());
                }
            }

            @Override
            public void onFailure(@NotNull Call<WanAndroidHttpResult<List<DataBean>>> call, @NotNull Throwable t) {
                       callBack.onFailure(t.getMessage());
            }
        });
    }
}
